package com.shallwead.bna.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.shallwead.bna.BnA;
import com.shallwead.bna.R;
import com.shallwead.bna.object.AdBack;
import com.shallwead.bna.object.AdGcmMsg;
import com.shallwead.bna.util.Logger;
import com.shallwead.bna.util.image.ImageDownloader;

public class PushPopupDialog extends Activity implements
		android.view.View.OnClickListener {

	public Context mContext;
	public RelativeLayout adfront_layout, adfront_layout_bottom;
	public ImageView adfront_img, adfront_btn_close, adfront_btn_ok;

	public AdBack adBack;
	public long delay = 0;
	public Bitmap bm = null;
	public AdGcmMsg gcmMsg = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dialog_adpopup);

		bm = BnA.gcmImg;
		gcmMsg = BnA.gcmMsg;

		if (bm == null) {
			finish();
		}
		if (gcmMsg == null) {
			finish();
		}

		mContext = PushPopupDialog.this;
		adBack = AdGcmMsg.parseAdBack(gcmMsg);

		setLayout();
		init();
	}

	public void setLayout() {
		adfront_layout = (RelativeLayout) findViewById(R.id.adpopup_layout);
		adfront_layout_bottom = (RelativeLayout) findViewById(R.id.adpopup_layout_bottom);
		adfront_img = (ImageView) findViewById(R.id.adpopup_img);
		adfront_btn_close = (ImageView) findViewById(R.id.adpopup_btn_close);
		adfront_btn_ok = (ImageView) findViewById(R.id.adpopup_btn_ok);

		adfront_layout.setOnClickListener(PushPopupDialog.this);
		adfront_layout_bottom.setOnClickListener(PushPopupDialog.this);
		adfront_img.setOnClickListener(PushPopupDialog.this);
		adfront_btn_ok.setOnClickListener(PushPopupDialog.this);
		adfront_btn_close.setOnClickListener(PushPopupDialog.this);
	}

	public void init() {
		Log.e("Error", "Error == " + adBack.getName());
		adfront_img.setImageBitmap(bm);
		// ImageDownloader imgDownLoader = new ImageDownloader(mContext,
		// adBack.getImageUrl());
		// imgDownLoader.download(adBack.getImageUrl(), adfront_img);

	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == adfront_layout.getId() || id == R.id.adpopup_layout_bottom
				|| id == R.id.adpopup_img || id == R.id.adpopup_btn_ok) {
			BnA.goPushAd(mContext, adBack);
			finish();
		} else if (id == R.id.adpopup_btn_close) {
			finish();
		}

	}

}